package com.feicheng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.feicheng.common.constant.Constant;
import com.feicheng.common.exception.SystemException;
import com.feicheng.common.result.ResultInfo;
import com.feicheng.persistence.entity.Distributor;
import com.feicheng.persistence.service.IDistributorService;
import com.feicheng.service.IFDistributorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class FDistributorServiceImpl implements IFDistributorService {

    @Autowired
    IDistributorService distributorService;

    @Override
    public ResultInfo updateDistributor(Distributor distributor) {
        distributorService.updateById(distributor);
        return ResultInfo.success();
    }

    @Override
    public ResultInfo newDistributor(Distributor distributor) {
        distributorService.save(distributor);
        return ResultInfo.success();
    }

    @Override
    public ResultInfo getDistributorByPage(Integer current, Integer size, Distributor distributor) {

        QueryWrapper<Distributor> distributorQueryWrapper = new QueryWrapper<>();

        if (StringUtils.hasLength(distributor.getName())) {
            distributorQueryWrapper.like("name", distributor.getName());
        }

        if (StringUtils.hasLength(distributor.getAccount())) {
            distributorQueryWrapper.like("account", distributor.getAccount());
        }

        if (StringUtils.hasLength(distributor.getPassword())) {
            distributorQueryWrapper.eq("password", distributor.getPassword());
        }

        if (StringUtils.hasLength(distributor.getTel())) {
            distributorQueryWrapper.like("tel", distributor.getTel());
        }

        if (StringUtils.hasLength(distributor.getAddress())) {
            distributorQueryWrapper.like("address", distributor.getAddress());
        }

        if (null != distributor.getStatu()) {
            distributorQueryWrapper.eq("statu", distributor.getStatu());
        }

        distributorQueryWrapper.eq("d_flg", false);

        Page<Distributor> page = new Page<>(current, size);

        return ResultInfo.success(distributorService.page(page, distributorQueryWrapper));
    }

    @Override
    public ResultInfo checkAccountEx(String account) {
        Distributor distributor = new Distributor();
        distributor.setAccount(account);
        List<Distributor> distributors =  (List<Distributor>) getDistributor(distributor).get(Constant.RESULT.DATA);
        Boolean result = false;
        if (null != distributors && distributors.size() > 0) {
            result = true;
        }
        return ResultInfo.success(result);
    }

    @Override
    public ResultInfo getDistributor(Distributor distributor) {
        QueryWrapper<Distributor> distributorQueryWrapper = new QueryWrapper<>();

        if (StringUtils.hasLength(distributor.getAccount())) {
            distributorQueryWrapper.eq("account", distributor.getAccount());
        }

        if (StringUtils.hasLength(distributor.getPassword())) {
            distributorQueryWrapper.eq("password", distributor.getPassword());
        }

        if (null != distributor.getStatu()) {
            distributorQueryWrapper.eq("statu", distributor.getStatu());
        }

        distributorQueryWrapper.eq("d_flg", false);

        return ResultInfo.success(distributorService.list(distributorQueryWrapper));
    }

    @Override
    @Transactional
    public ResultInfo checkPermission(String account, String password) {

        Distributor distributor = new Distributor();
        distributor.setAccount(account);
        distributor.setPassword(password);

        List<Distributor> distributorList =
                (List<Distributor>) this.getDistributor(distributor).get(Constant.RESULT.DATA);

        if (null == distributorList || distributorList.size() == 0) {
            throw new SystemException("经销商不存在");
        } else if (distributorList.size() > 1) {
            for (Distributor distributorFail : distributorList) {
                distributorFail.setStatu(false);
                distributorService.updateById(distributorFail);
            }
            throw new SystemException("经销商账户非法，已将相关经销商账户全部冻结！");
        } else {
            Distributor currentDistributor = distributorList.remove(0);

            if (!currentDistributor.getStatu()) {
                throw new SystemException("当前经销商账户已经冻结，请联系管理员！");
            }

            return ResultInfo.success(currentDistributor);
        }


    }
}
